extends IEventActuator


class_name MountSetter
"""
为指定的结点挂载子结点
"""


## 需要挂载的对象
@export var finder_or_node: Node = null
## 生成子结点的工厂
@export var factory: INodeFactory = null
## 如果父结点为ItemContainer自动使用append方式执行
@export var is_appender: bool = true
## 是否需要挂载到finder返回的对象上
@export var is_add_child: bool = true
## 是否需要使用set_deferred执行
@export var is_deferred: bool = false


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "finder_or_node": IComponent.find_child.bind(self, func(x): return x is INodeFinder, finder_or_node),
        "factory": IComponent.find_child.bind(self, func(x): return x is INodeFactory, factory),
    }, super.__check_exports())

func get_wrapper() -> Node:
    return finder_or_node.find() if finder_or_node is INodeFinder else finder_or_node

func execute() -> void:
    var node: Node = get_wrapper()
    if node == null:
        return
    var child: Node = factory.create()
    if child == null:
        return
    if is_appender and node is ItemContainer:
        if is_deferred:
            node.call_deferred("append", child)
        else:
            node.append(child)
    if is_add_child:
        NodeHelper.add_child(node, child, is_deferred)
